package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.MesGetMat;
import com.ruoyi.system.domain.MesStock;
import com.ruoyi.system.mapper.MesStockMapper;
import com.ruoyi.system.service.IMesStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MesSordOutMapper;
import com.ruoyi.system.domain.MesSordOut;
import com.ruoyi.system.service.IMesSordOutService;

/**
 * 销售出库表Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-01
 */
@Service
public class MesSordOutServiceImpl implements IMesSordOutService 
{
    @Autowired
    private MesSordOutMapper mesSordOutMapper;

    @Autowired
    private IMesStockService mesStockService;

    @Autowired
    private MesStockMapper mesStockMapper;

    /**
     * 查询销售出库表
     * 
     * @param id 销售出库表主键
     * @return 销售出库表
     */
    @Override
    public MesSordOut selectMesSordOutById(Integer id)
    {
        return mesSordOutMapper.selectMesSordOutById(id);
    }

    /**
     * 查询销售出库表列表
     * 
     * @param mesSordOut 销售出库表
     * @return 销售出库表
     */
    @Override
    public List<MesSordOut> selectMesSordOutList(MesSordOut mesSordOut)
    {
        return mesSordOutMapper.selectMesSordOutList(mesSordOut);
    }

    /**
     * 新增销售出库表
     * 
     * @param mesSordOut 销售出库表
     * @return 结果
     */
    @Override
    public int insertMesSordOut(MesSordOut mesSordOut)
    {
        List<MesSordOut> mtrs = mesSordOut.getMtrs();

        for (MesSordOut mtr : mtrs) {
            //1：处理数据
            mtr.setSordId(mtr.getSordId());
            mtr.setSordOutTime(mtr.getSordOutTime());

            mtr.setProdId(mtr.getProdId());

            mtr.setProdNum(mtr.getProdNum());
            mtr.setLocaId(mtr.getLocaId());
            //2.sql插入
            mesSordOutMapper.insertMesSordOut(mtr);

            //3.更新库存数量
            MesStock stock = mesStockMapper.selectByItemId(mtr.getProdId());
            if (stock != null){
                stock.setStockNum(mtr.getProdNum() - stock.getStockNum());
                mesStockService.updateMesStock(stock);
            }
        }
        return 1;

        //return mesSordOutMapper.insertMesSordOut(mesSordOut);
    }

    /**
     * 修改销售出库表
     * 
     * @param mesSordOut 销售出库表
     * @return 结果
     */
    @Override
    public int updateMesSordOut(MesSordOut mesSordOut)
    {
        return mesSordOutMapper.updateMesSordOut(mesSordOut);
    }

    /**
     * 批量删除销售出库表
     * 
     * @param ids 需要删除的销售出库表主键
     * @return 结果
     */
    @Override
    public int deleteMesSordOutByIds(Integer[] ids)
    {
        return mesSordOutMapper.deleteMesSordOutByIds(ids);
    }

    /**
     * 删除销售出库表信息
     * 
     * @param id 销售出库表主键
     * @return 结果
     */
    @Override
    public int deleteMesSordOutById(Integer id)
    {
        return mesSordOutMapper.deleteMesSordOutById(id);
    }
}
